package com.example.test1;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
    private int i=5;
    @BindView(R.id.mian_iv)
    ImageView mMianIv;
    @BindView(R.id.main_tv)
    TextView mMainTv;
    @SuppressLint("HandlerLeak")
    Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (i>0){
                i--;
                mMainTv.setText(mS+i);
            }else {
                mTimer.cancel();
                finish();
                Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
            }
        }
    };
    Timer mTimer;
    String mS;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mS = mMainTv.getText().toString();
        mMainTv.setText(mS+i);
        timer();
        animaion();
    }

    private void animaion() {
        AlphaAnimation animation = new AlphaAnimation(0, 1);
        animation.setDuration(3000);
        animation.setFillAfter(true);
        mMianIv.setAnimation(animation);
    }

    private void timer() {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(1);
            }
        };
        mTimer = new Timer();
        mTimer.schedule(task,1000,1000);
    }
}
